package com.todo.delicacy.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.common.utils.StringUtils;
import com.todo.delicacy.infrastructure.entity.condition.FoodInfoCondition;
import com.todo.delicacy.infrastructure.entity.po.FoodInfoPO;
import com.todo.delicacy.infrastructure.mapper.FoodInfoMapper;
import com.todo.delicacy.infrastructure.repository.FoodInfoRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * (FoodInfo)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("foodInfoRepository")
public class FoodInfoRepositoryImpl implements FoodInfoRepository {
    @Resource
    private FoodInfoMapper foodInfoMapper;


    @Override
    public Boolean insertFoodInfoPO(FoodInfoPO foodInfoPO) {
        if (foodInfoPO == null){
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(foodInfoPO)){
            return false;
        }
        return foodInfoMapper.insertFoodInfoPO(foodInfoPO) > 0;
    }

    @Override
    public Boolean updateFoodInfoPOById(FoodInfoPO foodInfoPO) {
        if (foodInfoPO == null){
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(foodInfoPO)){
            return false;
        }
        return foodInfoMapper.updateFoodInfoPOById(foodInfoPO) > 0;
    }

    @Override
    public Boolean removeFoodInfoPOById(Long id) {
        if (id == null || id < 0){
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        return foodInfoMapper.removeFoodInfoPOById(id) > 0;
    }

    @Override
    public PageResult<FoodInfoPO> queryFoodInfoByCondition(FoodInfoCondition foodInfoCondition) {
        PageResult<FoodInfoPO> pageResult = new PageResult<>();
        if (foodInfoCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(foodInfoCondition.getPageIndex());
        pageResult.setPageSize(foodInfoCondition.getPageSize());
        long count = queryCountByCondition(foodInfoCondition);
        if (count <= 0) {
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, foodInfoCondition.getPageSize()));
        pageResult.setTotal(count);
        List<FoodInfoPO> foodInfoPOList = foodInfoMapper.queryFoodInfoByCondition(foodInfoCondition);
        if (CollectionUtils.isEmpty(foodInfoPOList)) {
            pageResult.setHasNext(false);
            return pageResult;
        }
        pageResult.setResults(foodInfoPOList);
        pageResult.setCurrentIndex(foodInfoPOList.get(0).getId());
        pageResult.setHasNext(foodInfoPOList.size() >= foodInfoCondition.getPageSize());
        return pageResult;
    }

    @Override
    public Long queryCountByCondition(FoodInfoCondition foodInfoCondition) {
        if (foodInfoCondition == null || foodInfoCondition.getPageSize() <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return foodInfoMapper.queryCountByCondition(foodInfoCondition);
    }

    @Override
    public FoodInfoPO queryFoodInfoPOById(Long id) {
        if (id == null || id < 0){
            return null;
        }
        return foodInfoMapper.queryFoodInfoPOById(id);
    }

    @Override
    public FoodInfoPO queryFoodInfoPOByRestaurantAndName(Long restaurantId, String foodName) {
        if (restaurantId == null || restaurantId < 0 || StringUtils.isBlank(foodName)){
            return null;
        }
        return foodInfoMapper.queryFoodInfoPOByRestaurantAndName(restaurantId, foodName);
    }

    @Override
    public List<FoodInfoPO> queryFoodInfoPOByRestaurantId(Long restaurantId) {
        if (restaurantId == null || restaurantId < 0){
            return Lists.newArrayList();
        }
        return foodInfoMapper.queryFoodInfoPOByRestaurantId(restaurantId);
    }

    @Override
    public Boolean removeFoodInfoByRestaurantId(Long restaurantId) {
        if (restaurantId == null || restaurantId < 0){
            return false;
        }
        return foodInfoMapper.removeFoodInfoByRestaurantId(restaurantId);
    }

    @Override
    public List<FoodInfoPO> queryFoodInfoListByName(String foodName, Long restaurantId) {
        if (StringUtils.isBlank(foodName)){
            return Collections.emptyList();
        }
        return foodInfoMapper.queryFoodInfoListByName(foodName,restaurantId);
    }

    private boolean baseFieldCheck(FoodInfoPO foodInfoPO) {
        if (foodInfoPO == null){
            return false;
        }
        if (foodInfoPO.getRestaurantId() == null || foodInfoPO.getName() == null || foodInfoPO.getFoodType() == null){
            log.error("FoodInfoRepository#baseFieldCheck 核心字段校验失败，数据为空，操作失败 foodInfoPO：【{}】", JSONObject.toJSONString(foodInfoPO));
            return false;
        }
        return true;
    }
}
